import 'package:enterprise_v6_app/app/components/special_operation/custom_select_function.dart';
import 'package:enterprise_v6_app/app/components/toast_widget.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/get_dict_all_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/product_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/smart_warehouse/warehouse_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/smart_warehouse/smart_warehouse_controller.dart';
import 'package:get/get.dart';

class AddProductsController extends GetxController {
  /// 产品实体
  ProductEntity productEntity = ProductEntity();

  /// 产品类别字典数据
  List<GetDictAllEntity> categoryDictList = [];

  /// 产品级别字典数据
  List<GetDictAllEntity> levelDictList = [];

  /// 组合类型字典数据
  List<GetDictAllEntity> typeDictList = [];

  /// 燃放效果字典数据
  List<GetDictAllEntity> firingEffectsDictList = [];

  /// 燃放安全距离
  List<GetDictAllEntity> safeDistanceDictList = [];

  @override
  void onInit() {
    super.onInit();
    initData();
  }

  /// 初始化数据
  Future<void> initData() async {
    categoryDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '511410025r9dIpFIp');
    levelDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '5114100250JSrvT3J');
    typeDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '511410025KVXAR49f');
    firingEffectsDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '511410025eBr9Y9tP');
    safeDistanceDictList = await DictDataInfo.getDictionaryDataByTree(dictionaryType: '511410025kNHKwdYi');
  }

  /// 产品类别选择
  void showProductCategory() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '产品类别选择',
      dataList: categoryDictList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.productCategory = value.first.getId();
        productEntity.productCategoryName = value.first.getShowName();
        update();
      },
    );
  }

  /// 产品级别选择
  void showProductLevel() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '产品级别选择',
      dataList: levelDictList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.productLevel = value.first.getId();
        productEntity.productLevelName = value.first.getShowName();
        update();
      },
    );
  }

  /// 组合类型选择
  void showCombinationType() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '组合类型选择',
      dataList: typeDictList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.combinationType = value.first.getId();
        productEntity.combinationTypeName = value.first.getShowName();
        update();
      },
    );
  }

  /// 燃放效果选择
  void showFiringEffects() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '燃放效果选择',
      dataList: firingEffectsDictList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.firingEffects = value.first.getId();
        productEntity.firingEffectsName = value.first.getShowName();
        update();
      },
    );
  }

  /// 燃放安全距离选择
  void showSafeDistance() {
    CustomSelectFunction.selectMultipleBottomSheet<GetDictAllEntity>(
      title: '安全距离选择',
      dataList: safeDistanceDictList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.safeDistance = value.first.getId();
        productEntity.safeDistanceName = value.first.getShowName();
        update();
      },
    );
  }

  /// 仓库选择
  void showWarehouse() {
    // 获取仓库列表
    final warehouseList = Get.find<SmartWarehouseController>().warehouseList;
    CustomSelectFunction.selectMultipleBottomSheet<WarehouseEntity>(
      title: '仓库选择',
      dataList: warehouseList,
      isMultiple: false,
      onCallBack: (value) {
        productEntity.warehouseId = value.first.getId();
        productEntity.wareHouseName = value.first.getShowName();
        update();
      },
    );
  }

  /// 获取体积
  void getVolume() {
    final double l = productEntity.productLong!;
    final double w = productEntity.productWidth!;
    final double h = productEntity.productTall!;
    productEntity.volume = double.parse((l * w * h).toStringAsFixed(2));
    update();
  }

  /// 新增提交
  Future<void> addSubmit() async {
    if (productEntity.name!.isEmpty) {
      AppToast.showWarning('请输入产品名称!');
      return;
    }
    if (productEntity.productCategory!.isEmpty || productEntity.productCategoryName!.isEmpty) {
      AppToast.showWarning('请选择产品类别!');
      return;
    }
    if (productEntity.productLevel!.isEmpty || productEntity.productLevelName!.isEmpty) {
      AppToast.showWarning('请选择产品级别!');
      return;
    }
    if (productEntity.combinationQuantity == 0) {
      AppToast.showWarning('请输入组合发数!');
      return;
    }
    if (productEntity.combinationType!.isEmpty || productEntity.combinationTypeName!.isEmpty) {
      AppToast.showWarning('请选择组合类型!');
      return;
    }
    if (productEntity.dosage == 0) {
      AppToast.showWarning('请输入单筒药量!');
      return;
    }
    if (productEntity.boxContent == 0) {
      AppToast.showWarning('请输入箱含量!');
      return;
    }
    if (productEntity.netWeight == 0) {
      AppToast.showWarning('请输入毛重!');
      return;
    }
    if (productEntity.totalDosage == 0) {
      AppToast.showWarning('请输入总药量!');
      return;
    }
    if (productEntity.firingEffects!.isEmpty || productEntity.firingEffectsName!.isEmpty) {
      AppToast.showWarning('请选择燃放效果!');
      return;
    }
    if (productEntity.volume == 0) {
      AppToast.showWarning('请完善体积数据!');
      return;
    }
    if (productEntity.minimumAgeRequirement == 0) {
      AppToast.showWarning('请输入燃放者最低年龄要求!');
      return;
    }
    if (productEntity.safeDistance!.isEmpty || productEntity.safeDistanceName!.isEmpty) {
      AppToast.showWarning('请选择燃放安全距离!');
      return;
    }
    if (productEntity.warehouseId!.isEmpty || productEntity.wareHouseName!.isEmpty) {
      AppToast.showWarning('请选择存放仓库!');
      return;
    }
    if (productEntity.videoUrl!.isEmpty) {
      AppToast.showWarning('请上传产品视角视频!');
      return;
    }

    // 获取产品列表接口
    final res = await HttpUtil().generatePostRequest(Interface.addProduct, data: productEntity.toJson()).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        Get.back(closeOverlays: true);
        AppToast.showSuccess('新增成功!');
      }
    });
  }
}
